package love.yuanmengrobot.qianyugarden.service.impl;

import love.yuanmengrobot.qianyugarden.dao.FavoritesDao;
import love.yuanmengrobot.qianyugarden.dao.YuanwenDao;
import love.yuanmengrobot.qianyugarden.entity.Favorites;
import love.yuanmengrobot.qianyugarden.entity.Yuanwen;
import love.yuanmengrobot.qianyugarden.service.BrowseArticlesService;
import love.yuanmengrobot.qianyugarden.service.FavoritesService;

import love.yuanmengrobot.qianyugarden.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 收藏夹(Favorites)表服务实现类
 *
 * @author makejava
 * @since 2021-07-13 14:28:13
 */
@Service
public class FavoritesServiceImpl implements FavoritesService {
    private static final String DEFAULT_FOLD_NAME = "默认收藏夹";

    @Autowired
    FavoritesDao favoritesDao;

    @Autowired
    YuanwenDao yuanwenDao;

    @Autowired
    BrowseArticlesService browseArticlesService;

    /**
     * 通过ID查询单条数据
     *
     * @param objectid 主键
     * @return 实例对象
     */
    @Override
    public Favorites queryById(String objectid) {
        return this.favoritesDao.queryById(objectid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Favorites> queryAllByLimit(int offset, int limit) {
        return this.favoritesDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param favorites 实例对象
     * @return 实例对象
     */
    @Override
    public Favorites insert(Favorites favorites) {
        /*构建判断重复对象*/
        Favorites option = new Favorites();
        option.setUserid(favorites.getUserid());
        String name = favorites.getFavoritename();
        name = ("".equals(name) || name == null) ? DEFAULT_FOLD_NAME : name;
        option.setFavoritename(name);

        List<Favorites> list = favoritesDao.queryAll(option);
        if (list == null || list.size() == 0) {
            /*未重复，插入数据*/
            favorites.setFavoritename(name);
            this.favoritesDao.insert(favorites);
            return favorites;
        }
        return null;
    }

    /**
     * 修改数据
     *
     * @param favorites 实例对象
     * @return 实例对象
     */
    @Override
    public Favorites update(Favorites favorites) {
        this.favoritesDao.update(favorites);
        return this.queryById(favorites.getObjectid());
    }

    /**
     * 通过主键删除数据
     *
     * @param objectid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String objectid) {
        return this.favoritesDao.deleteById(objectid) > 0;
    }

    /**
     * 获取收藏夹菜单
     *
     * @param id 用户ID
     * @return 收藏夹菜单集合
     */
    @Override
    public List<Favorites> getMenu(String id) {
        Favorites favorites = new Favorites();
        favorites.setUserid(id);
        favorites.setRelationid("");
        favorites.setParentid("");
        List<Favorites> list = favoritesDao.queryAll(favorites);
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Favorites favor = (Favorites) iterator.next();
            String name = favor.getFavoritename();
            if ("".equals(name) || name == null) {
                iterator.remove();
            }
        }
        return list;
    }

    /**
     * 获取收藏夹内收藏的内容
     *
     * @param id 收藏夹ID
     * @return 收藏园文LIST
     */
    @Override
    public List<Yuanwen> getContent(String id) {
        List<Yuanwen> yuanWenList = new ArrayList<>();

        /*查询收藏夹指向的内容ID*/
        Favorites favorites = new Favorites();
        favorites.setParentid(id);
        List<Favorites> favoritesList = favoritesDao.queryAll(favorites);

        for (Favorites f : favoritesList) {
            yuanWenList.add(yuanwenDao.queryById(f.getRelationid()));
        }
        return yuanWenList;
    }

    @Override
    public Boolean addFavorites(String objectId, String favorite, String userId) {

        Favorites favorites = new Favorites();

        favorites.setUserid(userId);
        if("默认收藏夹".equals(favorite)){
            favorites.setFavoritename(favorite);
            List<Favorites> favorites1 = favoritesDao.queryAll(favorites);
            favorites.setObjectid(RandomUtils.createIdOX("FA"));
            if(favorites1.size()>0){
                favorites.setFavoritename(null);
                favorites.setParentid(favorites1.get(0).getObjectid());
                favorites.setRelationid(objectId);
                favoritesDao.insert(favorites);
            }else {
                favorites.setFavoritename(favorite);
                favoritesDao.insert(favorites);


                Favorites favorites2 = new Favorites();
                favorites2.setParentid(favorites.getObjectid());
                favorites2.setRelationid(objectId);
                favorites2.setObjectid(RandomUtils.createIdOX("FA"));
                favorites2.setUserid(userId);
                favoritesDao.insert(favorites2);
            }
        }else {
            favorites.setObjectid(RandomUtils.createIdOX("FA"));
            favorites.setRelationid(objectId);
            favorites.setParentid(favorite);
            favoritesDao.insert(favorites);
        }


        return browseArticlesService.collects(userId,objectId);

    }


    public Boolean noCollect(String objectId, String userId) {
        Favorites favorites = new Favorites();
        favorites.setUserid(userId);
        favorites.setRelationid(objectId);
        System.out.println(favoritesDao.deleteByObject(favorites));
        return browseArticlesService.collects(userId,objectId);
    }


}
